/*
* @(#)ListPersonalHomeAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.framework.web;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.alert.AlertOperationConstant;
import com.dcivision.alert.bean.MtmUpdateAlertRecipient;
import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertLogAction;
import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.alert.bean.UpdateAlertType;
import com.dcivision.alert.core.AlertManager;
import com.dcivision.alert.dao.MtmUpdateAlertRecipientDAObject;
import com.dcivision.alert.dao.UpdateAlertDAObject;
import com.dcivision.alert.dao.UpdateAlertSystemLogDAObject;
import com.dcivision.alert.dao.UpdateAlertTypeDAObject;
import com.dcivision.audit.core.AuditTrailManager;
import com.dcivision.calendar.bean.CalendarRecord;
import com.dcivision.calendar.dao.CalendarRecordDAObject;
import com.dcivision.calendar.dao.CalendarRecurDAObject;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.dao.DmsRootDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.user.UserHomePreferenceConstant;
import com.dcivision.user.bean.PersonalHomePreference;
import com.dcivision.user.core.PreferenceManager;
import com.dcivision.user.dao.PersonalHomePreferenceDAObject;
import com.dcivision.workflow.dao.WorkflowProgressDAObject;

/**
 * <p>Class Name:       ListSysUserDefinedIndexAction.java    </p>
 * <p>Description:      The list action class for Personal Home.jsp</p>
 *
 *    @author           Zoe Shum
 *    @company          DCIVision Limited
 *    @creation date    31/10/2003
 *    @version          $Revision: 1.47.2.7 $
 */

public class ListPersonalHomeAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.47.2.7 $";

  //Personal Home List Type constants.
  public final static String VAR_ALERT_CALENDARTOLIST = GlobalConstant.HOME_CALENDAR_TODO_LIST;
  public final static String VAR_ALERT_CALENDAREVENTLIST = GlobalConstant.HOME_CALENDAR_EVENT_LIST;
  public final static String VAR_ALERT_CALENDARMEETINGLIST = GlobalConstant.HOME_CALENDAR_MEETING_LIST;
  public final static String VAR_ALERT_SYSTEM_LOG = GlobalConstant.HOME_SYSTEM_LOG_LIST;
  public final static String VAR_RECENTLY_ACCESS = GlobalConstant.HOME_RECENTLY_ACCESS_LIST;
  public final static String VAR_WORKFLOW_TASK_LIST = GlobalConstant.HOME_WORKFLOW_TASK_LIST;
  public final static String VAR_WORKFLOW_TRACKING_LIST = GlobalConstant.HOME_WORKFLOW_TRACKING_LIST;

  public final static String PREFERENCE_3_CHANNELLIST_CONFIG_STR = PersonalHomePreference.CALENDAR_TODO_LIST+"|"+PersonalHomePreference.TOOLS+"|"+PersonalHomePreference.SYSTEM_LOGS;
  /**
   *  Constructor - Creates a new instance of ListPersonalHomeAction and define the default listName.
   */
  public ListPersonalHomeAction() {
    super();
    this.setListName("alertSystemLogList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  
  public String getMajorDAOClassName() {
    return(null);
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  
  public String getFunctionCode() {
    return(null);
  }

  /**
   * setPageTitle
   * set the extend page title and page path.
   * default page path/title will be created by navmode and functionCode
   */
  
  public void setPageTitle(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping, ActionForward actionForward){
	  String extendTitle=MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "common.label.home");
	  request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE, extendTitle);
	  request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE_SHOW_ONLY, new Boolean(true));
    
  }
  
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    /*
     * joan.xiong add code for bug EIP-172 by 2006/10/12
     */
    if(!Utility.isEmpty(request.getSession().getAttribute(org.apache.struts.Globals.MESSAGE_KEY))){
        //&& request.getSession().getAttribute(org.apache.struts.Globals.MESSAGE_KEY).equals("dms.message.records_rename")){
      this.addMessage(request, (String)request.getSession().getAttribute(org.apache.struts.Globals.MESSAGE_KEY));
      request.getSession().removeAttribute(org.apache.struts.Globals.MESSAGE_KEY);
    }
    if(!Utility.isEmpty(request.getSession().getAttribute(org.apache.struts.Globals.ERROR_KEY))){
      this.addError(request, (String)request.getSession().getAttribute(org.apache.struts.Globals.ERROR_KEY));
      request.getSession().removeAttribute(org.apache.struts.Globals.ERROR_KEY);
    }
    /*
     * joan.xiong add code end
     */
    ListPersonalHomeForm listForm = (ListPersonalHomeForm) form;
    String opMode = listForm.getOpMode();
    log.debug("opMode = " + opMode);

    // Set function code if necessary.
    if (!Utility.isEmpty(listForm.getCurFunctionCode())) {
      this.setFunctionCode(listForm.getCurFunctionCode());
    }

    // Set the function code for current action.
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {return forward;}
    if (GlobalConstant.OP_MODE_LIST_DELETE.equals(listForm.getOpMode())) {
      try {
        this.deleteScheduleListData(mapping, listForm, request, response);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
    }

    if (AlertOperationConstant.READ_OPERATION.equals(listForm.getOpMode())||
        AlertOperationConstant.UNREAD_OPERATION.equals(listForm.getOpMode())
        ) {
      try {
        this.updateReadOperationLog(mapping, listForm, request, response);
        this.commit(request);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
    }

    // Get current user's disable view channels
    this.getCurrentUserDisableViewChannels((AbstractSearchForm)form,request);

    return super.execute(mapping, form, request, response);
  }

  /**
   *  Method getListData() - retrieve the appropriate item list for current page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws Exception if cannot get the list data from DAO object
   */
  
  public synchronized void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    ListPersonalHomeForm listHomeForm = (ListPersonalHomeForm)form;
    listHomeForm.setbAccessCalendar(this.getSessionContainer(request).hasAccessRight(SystemFunctionConstant.SCHEDULE_CALENDAR, "R"));
    listHomeForm.setAccessRecentlyAccessList(this.getSessionContainer(request).hasAccessRight(SystemFunctionConstant.DMS_PUBLIC_FOLDER, "R"));
    listHomeForm.setAccessWorkflowTaskList(this.getSessionContainer(request).hasAccessRight(SystemFunctionConstant.WORKFLOW_TASK, "R"));
    listHomeForm.setAccessWorkflowTrackingList(this.getSessionContainer(request).hasAccessRight(SystemFunctionConstant.WORKFLOW_TRACK, "R"));
    listHomeForm.setAccessSystemLogs(this.getSessionContainer(request).hasAccessRight(SystemFunctionConstant.MESSAGE_INBOX, "R"));
    request.setAttribute("alertSystemLogList", new ArrayList(0));
    /*
    Map channelMap = new HashMap();//for display the channel req

    // According to the attribute name to call the corresponding list retrieval function for personal home.
    if (VAR_ALERT_SYSTEM_LOG.equals(listHomeForm.getCurrListType())) {
      log.debug("Getting System Log...");
      listHomeForm.setSystemLogsCurStartRowNo(form.getCurStartRowNo());
    }
    else if (VAR_RECENTLY_ACCESS.equals(listHomeForm.getCurrListType())) {
      log.debug("Getting Recently Access list...");
      listHomeForm.setRecentlyAccessCurStartRowNo(form.getCurStartRowNo());
    }
    else if (VAR_WORKFLOW_TASK_LIST.equals(listHomeForm.getCurrListType())) {
      log.debug("Getting Workflow task list...");
      listHomeForm.setWorkflowTaskCurStartRowNo(form.getCurStartRowNo());
    }
    else if (VAR_WORKFLOW_TRACKING_LIST.equals(listHomeForm.getCurrListType())) {
      log.debug("Getting Workflow tracking list...");
      listHomeForm.setWorkflowTrackingCurStartRowNo(form.getCurStartRowNo());
    }

    // Always search from the begin for other channels exception system logs (message inbox)
    listHomeForm.setCurStartRowNo("1");
    listHomeForm.setPageOffset(SystemParameterFactory.getSystemParameter("display.channel_todo_offset"));
    if(listHomeForm.getbAccessCalendar() && (listHomeForm.getDisableViewChannels().indexOf(PersonalHomePreference.CALENDAR_TODO_LIST)==-1)&&checkIfgetListByChannelName(PersonalHomePreference.CALENDAR_TODO_LIST,request)){
      long startTime = (new java.util.Date()).getTime();
      List calendarToList=this.getCalendarToList(request, listHomeForm);
      request.setAttribute(ListPersonalHomeAction.VAR_ALERT_CALENDARTOLIST , calendarToList);
      log.debug("calendarToList = "+calendarToList.size()+" ("+((new java.util.Date()).getTime()-startTime)/1000+"s)");
    }

    listHomeForm.setCurStartRowNo("1");
    listHomeForm.setPageOffset(SystemParameterFactory.getSystemParameter("display.channel_meeting_event_offset"));
    if (listHomeForm.getbAccessCalendar() && (listHomeForm.getDisableViewChannels().indexOf(PersonalHomePreference.CALENDAR_EVENT_LIST)==-1)&&checkIfgetListByChannelName(PersonalHomePreference.CALENDAR_EVENT_LIST,request)) {
      long startTime = (new java.util.Date()).getTime();
      List calendarEventList=this.getCalendarEventList(request, listHomeForm);
      request.setAttribute(ListPersonalHomeAction.VAR_ALERT_CALENDAREVENTLIST , calendarEventList);
      log.debug("calendarEventList = "+calendarEventList.size()+" ("+((new java.util.Date()).getTime()-startTime)/1000+"s)");
    }

    listHomeForm.setCurStartRowNo("1");
    listHomeForm.setPageOffset(SystemParameterFactory.getSystemParameter("display.channel_meeting_event_offset"));
    if (listHomeForm.getbAccessCalendar() && (listHomeForm.getDisableViewChannels().indexOf(PersonalHomePreference.CALENDAR_MEETING_LIST)==-1)&&checkIfgetListByChannelName(PersonalHomePreference.CALENDAR_MEETING_LIST,request)) {
      long startTime = (new java.util.Date()).getTime();
      List calendarMeetingList=this.getCalendarMeetingList(request, listHomeForm);
      request.setAttribute(ListPersonalHomeAction.VAR_ALERT_CALENDARMEETINGLIST , calendarMeetingList);
      log.debug("calendarMeetingList = "+calendarMeetingList.size()+" ("+((new java.util.Date()).getTime()-startTime)/1000+"s)");
    }

    listHomeForm.setCurStartRowNo(form.getCurStartRowNo());
    listHomeForm.setPageOffset(SystemParameterFactory.getSystemParameter("display.channel_system_log_offset"));
    if (listHomeForm.getAccessSystemLogs() && (listHomeForm.getDisableViewChannels().indexOf(PersonalHomePreference.SYSTEM_LOGS)==-1)&&checkIfgetListByChannelName(PersonalHomePreference.SYSTEM_LOGS,request)){
      long startTime = (new java.util.Date()).getTime();
      List sysLogList = this.getSystemLogList(request, listHomeForm);
      request.setAttribute(ListPersonalHomeAction.VAR_ALERT_SYSTEM_LOG, sysLogList);
      log.debug("sysLogList = "+sysLogList.size()+" ("+((new java.util.Date()).getTime()-startTime)/1000+"s)");
    }else{
      // Put a dummy message inbox list into request
      request.setAttribute(ListPersonalHomeAction.VAR_ALERT_SYSTEM_LOG, new ArrayList());
    }

    listHomeForm.setCurStartRowNo("1");
    listHomeForm.setPageOffset(SystemParameterFactory.getSystemParameter("display.channel_recent_access_offset"));
    if(listHomeForm.getAccessRecentlyAccessList() && (listHomeForm.getDisableViewChannels().indexOf(PersonalHomePreference.RECENTLY_ACCESSED_DOC)==-1)&&checkIfgetListByChannelName(PersonalHomePreference.RECENTLY_ACCESSED_DOC,request)){
      long startTime = (new java.util.Date()).getTime();
      List recentlyAccessList = this.getRecentlyAccessList(request, listHomeForm);
      request.setAttribute(ListPersonalHomeAction.VAR_RECENTLY_ACCESS , recentlyAccessList);
      log.debug("recentlyAccessList = "+recentlyAccessList.size()+" ("+((new java.util.Date()).getTime()-startTime)/1000+"s)");
    }

    listHomeForm.setCurStartRowNo("1");
    listHomeForm.setPageOffset(SystemParameterFactory.getSystemParameter("display.channel_workflow_task_offset"));
    if(listHomeForm.getAccessWorkflowTaskList() && (listHomeForm.getDisableViewChannels().indexOf(PersonalHomePreference.PENDING_WORKFLOW_TASK)==-1)&&checkIfgetListByChannelName(PersonalHomePreference.PENDING_WORKFLOW_TASK,request)){
      long startTime = (new java.util.Date()).getTime();
      List workflowTaskList = this.getWorkflowTaskList(request, listHomeForm);
      request.setAttribute(ListPersonalHomeAction.VAR_WORKFLOW_TASK_LIST, workflowTaskList);
      log.debug("workflowTaskList = "+workflowTaskList.size()+" ("+((new java.util.Date()).getTime()-startTime)/1000+"s)");
    }

    listHomeForm.setCurStartRowNo("1");
    listHomeForm.setPageOffset(SystemParameterFactory.getSystemParameter("display.channel_workflow_track_offset"));
    if(listHomeForm.getAccessWorkflowTrackingList() && (listHomeForm.getDisableViewChannels().indexOf(PersonalHomePreference.WORKFLOW_TRACKING_LIST)==-1)&&checkIfgetListByChannelName(PersonalHomePreference.WORKFLOW_TRACKING_LIST,request)){
      long startTime = (new java.util.Date()).getTime();
      List workflowTrackingList = this.getWorkflowTrackingList(request, listHomeForm);
      request.setAttribute(ListPersonalHomeAction.VAR_WORKFLOW_TRACKING_LIST, workflowTrackingList);
      log.debug("workflowTrackingList = "+workflowTrackingList.size()+" ("+((new java.util.Date()).getTime()-startTime)/1000+"s)");
    }*/
  }

  protected synchronized List getSystemLogList(HttpServletRequest request, ListPersonalHomeForm listHomeForm) throws ApplicationException{
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    List result = new ArrayList();
    if (listHomeForm.getAccessSystemLogs()) {
      AlertManager alertManager = new AlertManager(sessionContainer, conn);
      // According to the attribute name to call the corresponding function for personal home.
      log.debug("Getting System Log...");
      result = alertManager.getSystemLogListByUserRecordID(sessionContainer.getUserRecordID(), listHomeForm);
    }
    return result;
  }

  //Get Calendar ToDO List
  protected synchronized List getCalendarToList(HttpServletRequest request, ListPersonalHomeForm listHomeForm) throws ApplicationException{
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    List result = new ArrayList();
    //Update By Jim.Zhou    2006/10/16    EIP-693
    //if (listHomeForm.getAccessSystemLogs()) {
    if (listHomeForm.getbAccessCalendar()) {
      AlertManager alertManager = new AlertManager(sessionContainer, conn);
      // According to the attribute name to call the corresponding function for personal home.
      log.debug("Getting Calendar ToDo list...");
      result = alertManager.getCalendarToListByUserRecordID(sessionContainer.getUserRecordID(), listHomeForm);
    }
    return result;
  }

  //Get Calendar Event List
  protected synchronized List getCalendarEventList(HttpServletRequest request, ListPersonalHomeForm listHomeForm) throws ApplicationException{
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    List result = new ArrayList();
    //Update By Jim.Zhou    2006/10/16    EIP-693
    //if (listHomeForm.getAccessSystemLogs()) {
    if (listHomeForm.getbAccessCalendar()) {
      AlertManager alertManager = new AlertManager(sessionContainer, conn);
      // According to the attribute name to call the corresponding function for personal home.
      log.debug("Getting Calendar Event list...");
      result = alertManager.getCalendarEventListByUserRecordID(sessionContainer.getUserRecordID(), listHomeForm);
    }
    return result;
  }

  //Get Calendar Meeting List
  protected synchronized List getCalendarMeetingList(HttpServletRequest request, ListPersonalHomeForm listHomeForm) throws ApplicationException{
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    CalendarRecordDAObject calendarRecordDAO = new CalendarRecordDAObject(
        sessionContainer, conn);
    List result = new ArrayList();
    List tempMeetingList =  new ArrayList();
    CalendarRecord calendarRecord;
    List systemLogList = new ArrayList();
    ArrayList invitaCalendarList = new ArrayList();
    try {
      UpdateAlertSystemLogDAObject alertSystemDAO = new UpdateAlertSystemLogDAObject(sessionContainer, conn);
      AlertManager alertManager = new AlertManager(sessionContainer, conn);
      List allRelatedUpdateAlertID = alertManager.getInvtatRelatedUpdateAlertIDListByUserID(sessionContainer.getUserRecordID());

      if (!Utility.isEmpty(allRelatedUpdateAlertID)) {

        systemLogList = alertSystemDAO.getReplySystemLogList(allRelatedUpdateAlertID, "Y");
        alertSystemDAO=null;
      }
    }catch (Exception e){
      log.error(e, e);
    }
     String[] acceptCalendarIDs=new String[systemLogList.size()];
     if(systemLogList.size()>0){
        for(int i=0 ;i< systemLogList.size();i++){
            if(!Utility.isEmpty(systemLogList.get(i))){
                UpdateAlertSystemLog updateAlert = (UpdateAlertSystemLog)systemLogList.get(i);
                acceptCalendarIDs[i] = updateAlert.getObjectID().toString();
            }
        }
     }
     result = calendarRecordDAO.getOwnerAndAcceptedListByDatePeriodAndEventType(sessionContainer.getUserRecordID(),new Timestamp(new Date().getTime()),null,new String[]{CalendarRecord.EVENT_TYPE_CALENDAR_MEETING,CalendarRecord.EVENT_TYPE_CALENDAR_EVENT},acceptCalendarIDs);
     return result;
  }

  protected synchronized List getRecentlyAccessList(HttpServletRequest request, ListPersonalHomeForm listHomeForm) throws ApplicationException{
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    List result = new ArrayList();
    if (listHomeForm.getAccessRecentlyAccessList()) {
//      AuditTrailManager auditTrailManager = new AuditTrailManager(sessionContainer, conn);
//      // According to the attribute name to call the corresponding function for personal home.
//      log.debug("Getting  Recently Access List ...");
//      result = (List)auditTrailManager.getDmsAccessList(GlobalConstant.HOME_RECENTLY_ACCESS_LIST, listHomeForm);

      if (!SystemParameterFactory.getSystemParameterBoolean(SystemParameterConstant.DMS_DISPLALY_NO_ACCESS_DOCUUMENT)){
        int pageSize = TextUtility.parseInteger(listHomeForm.getPageOffset());
        AuditTrailManager auditTrailManager = new AuditTrailManager(sessionContainer, conn);
        List tmpResult = null;

        DmsRootDAObject dmsRootDAO = new DmsRootDAObject(sessionContainer,this.getConnection(request));
        PermissionManager pm = sessionContainer.getPermissionManager();
        while (result.size() < pageSize){
          tmpResult = auditTrailManager.getDmsAccessList(GlobalConstant.HOME_RECENTLY_ACCESS_LIST, listHomeForm);
          if (tmpResult == null ||tmpResult.size() < 1) {
            break;
          }
          for (int i=0; tmpResult != null && tmpResult.size() > i; i++){
            if (result.size() >= pageSize) {
              break;
            }
            DmsDocument dmsDoc = (DmsDocument)tmpResult.get(i);
            DmsRoot dmsRoot = (DmsRoot)dmsRootDAO.getObjectByID(dmsDoc.getRootID());
            if (pm.hasAccessRight(conn,GlobalConstant.OBJECT_TYPE_DOCUMENT,dmsDoc.getID(),"R") || dmsRoot.getOwnerID().equals(this.getSessionContainer(request).getUserRecordID())){
              result.add(dmsDoc);
           }
          }
          listHomeForm.setCurStartRowNo(TextUtility.parseInteger(listHomeForm.getCurStartRowNo()) + pageSize + "");
        }
      } else {
        AuditTrailManager auditTrailManager = new AuditTrailManager(sessionContainer, conn);
        // According to the attribute name to call the corresponding function for personal home.
        log.debug("Getting  Recently Access List ...");
        result = auditTrailManager.getDmsAccessList(GlobalConstant.HOME_RECENTLY_ACCESS_LIST, listHomeForm);
      }
    }
    return result;
  }

  protected synchronized List getWorkflowTaskList(HttpServletRequest request, ListPersonalHomeForm listHomeForm) throws ApplicationException {
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    List result = new ArrayList();
    if (listHomeForm.getAccessWorkflowTaskList()) {
      WorkflowProgressDAObject workflowProgressDAO = new WorkflowProgressDAObject(sessionContainer, conn);
      // According to the attribute name to call the corresponding function for personal home.
      log.debug("Getting  Workflow Task List ...");
      // result = workflowProgressDAO.getListPendingForUser(listHomeForm);
      result = workflowProgressDAO.getPendingList4UserCanRead(listHomeForm);
    }
    return result;
  }

  protected synchronized List getWorkflowTrackingList(HttpServletRequest request, ListPersonalHomeForm listHomeForm) throws ApplicationException {
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    List result = new ArrayList();
    //Update By Jim.Zhou    2006/10/16    EIP-693
    //if (listHomeForm.getAccessWorkflowTaskList()) {
    if (listHomeForm.getAccessWorkflowTrackingList()) {
      WorkflowProgressDAObject workflowProgressDAO = new WorkflowProgressDAObject(sessionContainer, conn);
      // According to the attribute name to call the corresponding function for personal home.
      log.debug("Getting  Workflow Tracking List ...");
      result = workflowProgressDAO.getListTrackingByUserRecordID(listHomeForm);
    }
    return result;
  }

  protected synchronized void updateReadOperationLog(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException{
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    ListPersonalHomeForm listForm = (ListPersonalHomeForm) form;
    Integer systemLogID = listForm.getActionSystemLogID();

    if (systemLogID!=null) {
      AlertManager alertManager = new AlertManager(sessionContainer, conn);
      log.debug("OpMode:"+listForm.getOpMode());
      if (AlertOperationConstant.UNREAD_OPERATION.equals(listForm.getOpMode()) &&
        alertManager.checkForSystemLogActionByActionType(systemLogID, UpdateAlertLogAction.READ_ACTION)) {
        //Got READ record; Can go delete the READ reocrd to mark as un-read
        //alertManager.deleteLogActionBySystemLogID(systemLogID);
        alertManager.deleteLogActionBySystemLogIDCurActor(systemLogID);
      }
      else if(AlertOperationConstant.READ_OPERATION.equals(listForm.getOpMode())){
        //Go create read record
        UpdateAlertSystemLog systemLog = new UpdateAlertSystemLog();
        systemLog.setID(systemLogID);
        alertManager.createSystemLogAction(systemLog, UpdateAlertLogAction.READ_ACTION, null, null);
      }
    }
    listForm.setOpMode(null);
    listForm.setActionSystemLogID(null);
    try{}catch(Exception e){log.error(e, e);}finally{ conn=null;}
  }

  /**
   * Get current user's disable view channels.
   * @param form
   * @param request
   * @throws ApplicationException
   */
  protected synchronized void getCurrentUserDisableViewChannels(AbstractSearchForm form, HttpServletRequest request) throws ApplicationException {
    PersonalHomePreferenceDAObject personalPreferenceDAO = new PersonalHomePreferenceDAObject(
                this.getSessionContainer(request),this.getConnection(request));
    PersonalHomePreference personalPreference =
        (PersonalHomePreference)personalPreferenceDAO.getByObjectByUserRecordID(
                        this.getSessionContainer(request).getUserRecordID());
    PreferenceManager preferenceMg = new PreferenceManager(this.getSessionContainer(request),this.getConnection(request));

    ListPersonalHomeForm maintForm = (ListPersonalHomeForm)form;
    if (!Utility.isEmpty(personalPreference)) {
      if (!Utility.isEmpty(personalPreference.getDisableViewChannel())) {
        String channeldisableViewAndViewStr = preferenceMg.fitTheOldChannelConfig(personalPreference.getDisableViewChannel());
        String[] preferences = preferenceMg.getChannelPreference(channeldisableViewAndViewStr);
        maintForm.setDisableViewChannels(preferences[0]);
        maintForm.setAbleViewChannels(preferences[1]);
      } else {
        String[] preferences = preferenceMg.getChannelPreference(UserHomePreferenceConstant.SYSTEMPREFERENCESTR);
        maintForm.setDisableViewChannels(preferences[0]);
        maintForm.setAbleViewChannels(preferences[1]);
      }
    } else {
      String[] preferences = preferenceMg.getChannelPreference(UserHomePreferenceConstant.SYSTEMPREFERENCESTR);
      maintForm.setDisableViewChannels(preferences[0]);
      maintForm.setAbleViewChannels(preferences[1]);
    }
    //--------set the channel order--------------------
    List channelList = preferenceMg.getChannelSequence(maintForm.getAbleViewChannels());
    request.setAttribute("channelList", channelList);
  }
/*
  protected synchronized List getEnabledViewChannelListOrder(String enabledViewChannelsStr){
    String[] channels = enabledViewChannelsStr.split("\\|");
    List ls = null;
    ls = Arrays.asList(channels);
    Collections.sort(ls, new AjaxChannelPreferenceComparator());
    return ls;
  }
  protected synchronized String fitTheOldChannelConfig(String channeldisableViewAndViewStr){
    StringBuffer bstr = new StringBuffer();
    if(channeldisableViewAndViewStr.indexOf("_Y_")>=0||channeldisableViewAndViewStr.indexOf("_N_")>=0){
       bstr.append(channeldisableViewAndViewStr);
    }else{
      //--OLD CHANNEL CONFIG STR
      String[] channels = new String[]{PersonalHomePreference.CALENDAR_EVENT_LIST,
                                       PersonalHomePreference.CALENDAR_MEETING_LIST,
                                       PersonalHomePreference.CALENDAR_TODO_LIST,
                                       PersonalHomePreference.PENDING_WORKFLOW_TASK,
                                       PersonalHomePreference.RECENTLY_ACCESSED_DOC,
                                       PersonalHomePreference.SYSTEM_LOGS,
                                       PersonalHomePreference.TOOLS,
                                       PersonalHomePreference.WORKFLOW_TRACKING_LIST
      };
      for(int i=0;i<channels.length;i++){
        if(channeldisableViewAndViewStr.indexOf(channels[i])>=0){
          if(i==channels.length-1)
            bstr.append(channels[i]+"_N_5_1");
          else
            bstr.append(channels[i]+"_N_5_1|");
        }else{
          if(i==channels.length-1)
            bstr.append(channels[i]+"_Y_5_1");
          else
            bstr.append(channels[i]+"_Y_5_1|");
         }
       }
    }

    return bstr.toString();
  }

  protected synchronized String[] getChannelPreference(String channeldisableViewAndViewStr){
    String[] preferences = new String[]{"",""};
    if(channeldisableViewAndViewStr==null)
      return preferences;

    String[] tempChannelViewSytles = channeldisableViewAndViewStr.split("\\|");
    String disAbleViewStr="";
    String ableViewStr="";
    for(int i=0;i<tempChannelViewSytles.length;i++){
      if(tempChannelViewSytles[i].indexOf("_N_")==-1)
        ableViewStr=ableViewStr.equals("")?tempChannelViewSytles[i]:ableViewStr+"|"+tempChannelViewSytles[i];
      if(tempChannelViewSytles[i].indexOf("_Y_")==-1)
        disAbleViewStr=disAbleViewStr.equals("")?tempChannelViewSytles[i]:disAbleViewStr+"|"+tempChannelViewSytles[i];
    }
    preferences[0] = disAbleViewStr;
    preferences[1] = ableViewStr;
    return preferences;
  }*/
  
  /**
   * function deleteListData is to delete data in list.
   * @param mapping                        mapping for struts
   * @param form                           ActionForm for struts
   * @param request                        HttpServletRequest
   * @param response                       HttpServletResponse
   * @throws ApplicationException          Throws Exception if cannot get the list data from DAO object
   */
  public void deleteScheduleListData(ActionMapping mapping,AbstractSearchForm form,
                             HttpServletRequest request,
                             HttpServletResponse response)
      throws ApplicationException {

    Connection conn = null;
    try {
      String[] arg = form.getBasicSelectedID();
      String[] itemToDel = null;

      if (arg != null) {
        conn = this.getConnection(request);
        CalendarRecordDAObject calendarDAO = new CalendarRecordDAObject(this.
            getSessionContainer(request), this.getConnection(request));
        for (int i = 0; i < arg.length; i++) {
          itemToDel = TextUtility.splitString(arg[i], "|");
          if (itemToDel != null) {
            CalendarRecord calendarRecord = (CalendarRecord) calendarDAO.
                getObjectByID(new Integer(itemToDel[0]));
            if (CalendarRecord.EVENT_TYPE_CALENDAR_TODO.equals(itemToDel[1]) ||
                CalendarRecord.EVENT_TYPE_CALENDAR_EVENT.equals(itemToDel[1]) ||
                CalendarRecord.EVENT_TYPE_CALENDAR_MEETING.equals(itemToDel[1])) {
              calendarDAO.SoftDelete(new Integer(itemToDel[0]));
            }

            if (CalendarRecord.EVENT_TYPE_CALENDAR_MEETING.equals(itemToDel[1])) {
                AlertManager alertManager = new AlertManager(this.getSessionContainer(request), conn);
                calendarRecord = selectInviteMeeting(request,this.getSessionContainer(request),conn,calendarRecord);
                UpdateAlertDAObject updateAlertDAO = new UpdateAlertDAObject(this.
                    getSessionContainer(request), conn);
                UpdateAlert updateAlert = (UpdateAlert) updateAlertDAO.
                getByObjectTypeObjectID(UpdateAlert.CALENDAR_TYPE,
                                        new Integer(itemToDel[0]));

                if (calendarRecord.getNotifyWay()!=null){
                  updateUpdateAlert(request,this.getSessionContainer(request),conn,calendarRecord,alertManager,"D",true);


                }


              //REMINDER HANDLING
              if (calendarRecord.getReminderType() != null &&
                  (calendarRecord.
                   getReminderType().indexOf(UpdateAlert.EMAIL_NOTIFICATION) >=
                   0)) {
                AlertManager am = new AlertManager(this.getSessionContainer(
                    request), conn);
                UpdateAlert delUpdateAlert = new UpdateAlert();
                delUpdateAlert.setObjectType(UpdateAlert.CALENDAR_TYPE);
                delUpdateAlert.setObjectID(calendarRecord.getID());
                delUpdateAlert.setCreatorName(calendarRecord.getCreatorName());
                List userRecordIDList = new ArrayList(1);
                userRecordIDList.add(this.getSessionContainer(request).
                                     getUserRecordID());
                java.util.Date date = new java.util.Date(calendarRecord.
                    getDatetime().getTime());

                try {
                  am.delScheduleJobNotification(updateAlert);
                } catch (Exception e) {}
              }
            }
            //Recurring
            if (calendarRecord.getIsRecurrence() != null) {
              CalendarRecurDAObject calendarRecurDAO = new
                  CalendarRecurDAObject(this.getSessionContainer(request), conn);
              calendarRecurDAO.deleteByCalendarID(calendarRecord.getID());
            }
          }
        }
        this.commit(request);
      }
      conn = null;
      form.setOpMode(null);
      form.setBasicSelectedID(null);
    } catch (ApplicationException appEx) {
    } catch (Exception e) {
    }

  }
  /**
   * update or delete a meeting if the meeting have invitation ,update the updateAlertlog
   *
   *
   *
   * @param request
   * @param sessionContainer
   * @param request
   * @param connection
   * @param calendarRecord
   * @param am
   * @param type : "U"update,"D"delete
   * @throws ApplicationException
   */
  private CalendarRecord updateUpdateAlert(HttpServletRequest request,
      SessionContainer sessionContainer, Connection connection,
      CalendarRecord calendarRecord, AlertManager am, String type,boolean isModify)
      throws ApplicationException {
      String[] notifyWayString = (calendarRecord.getNotifyWay().split(","));
      UpdateAlertDAObject updateAlertDAO = new UpdateAlertDAObject(sessionContainer,
              connection);
      if (notifyWayString.length > 0) {

          UpdateAlert updateAlert = new UpdateAlert();

          if (calendarRecord.getUpdateAlertID() != null) {
              updateAlert = (UpdateAlert) updateAlertDAO.getObjectByID(calendarRecord.getUpdateAlertID());
          } else {
              if (notifyWayString.length > 0) {
                  updateAlert.setObjectType(UpdateAlert.CALENDAR_TYPE);
                  updateAlert.setObjectID(calendarRecord.getID());

                  StringBuffer str = new StringBuffer();

                  for (int i = 0; i < notifyWayString.length; i++) {
                      str.append(notifyWayString[i]).append(",");
                  }

                  //updateAlert.setNotifyWay(str.toString());
                  calendarRecord.setNotifyWay(str.toString());
              }
          }

          StringBuffer str = new StringBuffer();

          for (int i = 0; i < notifyWayString.length; i++) {
              str.append(notifyWayString[i]).append(",");
          }

          //updateAlert.setNotifyWay(str.toString());
          calendarRecord.setNotifyWay(str.toString());

          /*
                String[] userRecords = request.getParameterValues("userList");
                String[] userGroups = request.getParameterValues("groupList");
                String[] userRoles = request.getParameterValues("roleList");
          */
          //String[] userRecords = request.getParameterValues("userRecords");
          //String[] userRoles = request.getParameterValues("userRoles");
          //String[] userGroups = request.getParameterValues("userGroups");
          //get the notifyUserIDList
//        set  invite userRecords,userGroups,userRoles
          //get update alert recipient
          List recipientAry = new ArrayList();
          MtmUpdateAlertRecipientDAObject updateRecipientDAO = new MtmUpdateAlertRecipientDAObject(sessionContainer,
                  connection);

          // Get all recipients.
          String recipientType = null;
          String[] tmprecipientAry = new String[0];

          if (!Utility.isEmpty(calendarRecord.getUpdateAlertID())) {
              recipientAry = updateRecipientDAO.getByUpdateAlertID(calendarRecord.getUpdateAlertID());
          }

          tmprecipientAry = new String[recipientAry.size()];

          String[] userRecords = new String[recipientAry.size()];
          String[] userGroups = new String[recipientAry.size()];
          String[] userRoles = new String[recipientAry.size()];

          for (int i = 0; i < recipientAry.size(); i++) {
              MtmUpdateAlertRecipient tmprecipient = (MtmUpdateAlertRecipient) recipientAry.get(i);

              if (tmprecipient.getRecipientType().equals(UpdateAlert.USER_RECIPIENT)) {
                  userRecords[i] = tmprecipient.getRecipientID().toString();
              } else if (tmprecipient.getRecipientType().equals(UpdateAlert.GROUP_RECIPIENT)) {
                  userGroups[i] = tmprecipient.getRecipientID().toString();
              } else if (tmprecipient.getRecipientType().equals(UpdateAlert.ROLE_RECIPIENT)) {
                  userRoles[i] = tmprecipient.getRecipientID().toString();
              }
          }
          if (userRecords[0]==null) {
            userRecords=null;
          }
          if (userGroups[0]==null) {
            userGroups=null;
          }
          if (userRoles[0]==null) {
            userRoles=null;
          }
          List notifyUserIDList = am.getDistinctUserIDListByLists(userRecords,
                  userGroups, userRoles);

          //MtmUpdateAlertRecipient[] tmpRecipients=am.getRecipient(recipientType, userRecords, userRoles, userGroups);
          MtmUpdateAlertRecipient[] tmpRecipients = am.getRecipient(userRecords,
                  userGroups, userRoles);
          String[] actionTypes = new String[1];
          actionTypes[0] = "I";

          //      actionTypes[0] = "R";
          UpdateAlertType[] updateTypes = new UpdateAlertType[actionTypes.length];

          for (int i = 0; i < actionTypes.length; i++) {
              UpdateAlertType alertUpdateType = new UpdateAlertType();
              alertUpdateType.setUpdateAlertID(updateAlert.getID());
              alertUpdateType.setNotifyWay(str.toString());
              alertUpdateType.setActionType(actionTypes[i]);
              alertUpdateType.setNeedReply("Y");
              updateTypes[i] = alertUpdateType;
          }

          //update
          if ("U".equals(type)) {
              if (calendarRecord.getUpdateAlertID() != null) {
                  am.deleteUpdateAlert(updateAlert);
                  //am.createUpdateAlert(updateAlert, updateTypes, null,tmpRecipients);
                  am.createUpdateAlert(updateAlert,
                          updateTypes, tmpRecipients, null, null,
                          calendarRecord.getDatetime(), calendarRecord.getTitle(),
                          calendarRecord.getCreatorID(), notifyUserIDList);
                  if(isModify){
                    updateAlert.setObjectType(UpdateAlert.CALENDAR_MODIFY_TYPE);
                    //am.createUpdateAlert(updateAlert,
                          //updateTypes, tmpRecipients, null, null,
                          //calendarRecord.getDatetime(), calendarRecord.getTitle(),
                          //calendarRecord.getCreatorID(), notifyUserIDList);
                    am.createUpdateAlert(updateAlert, updateTypes, null,tmpRecipients);
                  }
                  //am.updateUpdateAlert(updateAlert, updateTypes, tmpRecipients, UpdateAlert.INVITE_TYPE);
              } else {
                am.createUpdateAlert(updateAlert,
                          updateTypes, tmpRecipients, null, null,
                          calendarRecord.getDatetime(), calendarRecord.getTitle(),
                          calendarRecord.getCreatorID(), notifyUserIDList);
                  //am.createUpdateAlert(updateAlert, updateTypes, null,tmpRecipients);
              }

              //delete
          } else {
              updateAlert.setObjectType(UpdateAlert.CALENDAR_INACTIVE_TYPE);
              am.createUpdateAlert(updateAlert, updateTypes, null,
                  tmpRecipients);
          }
      }else{
        UpdateAlert updateAlert = new UpdateAlert();

        if (calendarRecord.getUpdateAlertID() != null) {
            updateAlert = (UpdateAlert) updateAlertDAO.getObjectByID(calendarRecord.getUpdateAlertID());
            am.deleteUpdateAlert(updateAlert);
        }


      }

      return calendarRecord;
  }
  private CalendarRecord selectInviteMeeting(HttpServletRequest request,
      SessionContainer sessionContainer, Connection connection,
      CalendarRecord calendarRecord) throws ApplicationException {
      AlertManager alertManager = new AlertManager(sessionContainer,
              connection);
      UpdateAlert updateAlert = null;
      List alertList = new ArrayList();
      alertList = alertManager.listUpdateAlertByObjectTypeObjectID(UpdateAlert.CALENDAR_TYPE,
              calendarRecord.getID());

      if (alertList != null) {
          if (alertList.size() > 0) {
              updateAlert = (UpdateAlert) alertList.get(0);

              //calendarRecord.setNotifyWay( /*updateAlert.getNotifyWay()*/null);
              calendarRecord.setUpdateAlertID(updateAlert.getID());

              List actionTypes = new ArrayList();
              UpdateAlertTypeDAObject updateAlertTypeDAO = new UpdateAlertTypeDAObject(sessionContainer,
                      connection);
              actionTypes = updateAlertTypeDAO.getByUpdateAlertIDByOwnerID(updateAlert.getID(),
                      UpdateAlert.INVITE_TYPE, updateAlert.getCreatorID());

              //actionTypes = alertManager.listAlertUpdateTypeByUpdateAlertID(updateAlert.getID(), UpdateAlert.INVITE_TYPE);
              if (actionTypes != null) {
                  String[] actionString = new String[actionTypes.size()];

                  for (int i = 0; i < actionTypes.size(); i++) {
                      UpdateAlertType alertUpdateType = (UpdateAlertType) actionTypes.get(i); //getalertUpdateTypeList
                      calendarRecord.setNeedReply(alertUpdateType.getNeedReply());
                      calendarRecord.setNotifyWay(alertUpdateType.getNotifyWay());

                      UpdateAlertSystemLogDAObject updateSystemLogDAO = new UpdateAlertSystemLogDAObject(sessionContainer,
                              connection);
                      UpdateAlertSystemLog updateSystemLog = new UpdateAlertSystemLog();

                      updateSystemLog.setUpdateAlertTypeID(alertUpdateType.getID());
                      updateSystemLog.setActionUserID(sessionContainer.getUserRecordID());

                      UpdateAlertSystemLog newUpdateSystemLog = (UpdateAlertSystemLog) updateSystemLogDAO.getByUpdateAlertTypeIDActionUserID(updateSystemLog.getUpdateAlertTypeID(),
                              updateSystemLog.getActionUserID());

                      if (newUpdateSystemLog != null) {
                          request.getSession().setAttribute("replyType",
                              updateSystemLog.getActionReply());
                      } else {
                          request.getSession().setAttribute("replyType", "");
                      }
                  }
              }
          }
      }

      return calendarRecord;
  }
}